package com.example.jcservice.controller;

import com.example.jcservice.service.JwService;
import com.example.jcservice.stType.Resive.Jw.ExamInfo;
import com.example.jcservice.stType.Resive.Jw.JwUserInfo;
import com.example.jcservice.stType.Resive.Jw.LoginInfo;
import com.example.jcservice.stType.Resive.Jw.ScoresInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;


@RestController
public class JwController {
    @Resource//Spring依赖注入写法
    private JwService jwService;

    /**
     * 客户端在模拟发起登录请求前，先向这个接口发起请求，服务器再去模拟请求登录界面
     * 还没有登录 这张做是为了拿到 html 源码里面的__VIEWSTATE和这次请求的cookie
     * 因为要实现在客户端显示验证码和，服务器下一步的模拟登录需要__VIEWSTATE、cookie这两个值
     *
     * 这个接口还要负责把服务器请求的登录页面的cookie传回前端，这样前端带cookie就可以去请求验证码接口，绑定这次模拟登录请求
     * */
    @GetMapping("/jwloginbefore")
    public LoginInfo loginBefore() throws UnsupportedEncodingException {
        LoginInfo info = jwService.loginBefore();


        return info;
    }

    /**
     * 教务网获取验证码接口
     * */
    @GetMapping("/getcheckcode.jpg")
    //这里用Header存放参数是因为前端使用Glide请求在线图片URL目前只能添加header
    public void getCheckCode(HttpServletResponse response, @RequestHeader(name = "Session") String session) throws Exception {
        System.out.println(session);

        InputStream myStream = jwService.getCheckCode(session);
        response.setContentType("image/Gif; charset=gb2312");
        OutputStream outputStream = response.getOutputStream();
        byte[] buffer = new byte[1024];
        int len;
        while ((len = myStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, len);
        }
        myStream.close();

    }

    /**
     * 教务网登录接口
     * */
    @PostMapping("/jwlogin")
    public JwUserInfo login(@RequestBody LoginInfo loginInfo){
        System.out.println("传入参数 学号：" + loginInfo.getUsername());
        System.out.println("传入参数 密码：" + loginInfo.getPassword());
        System.out.println("传入参数 验证码：" + loginInfo.getCheckCode());
        System.out.println("传入参数 session：" + loginInfo.getSessionId());

        JwUserInfo userInfo = jwService.login(loginInfo.getUsername(),loginInfo.getPassword(),loginInfo.getCheckCode(),loginInfo.get__VIEWSTATE(),loginInfo.getSessionId());

        return userInfo;
    }

    /**
     * 考试安排查询接口
     *
     * @return*/
    @PostMapping("/getexaminfo")
    public ExamInfo getExamInfo(@RequestBody LoginInfo loginInfo) throws UnsupportedEncodingException {
        ExamInfo examInfo = jwService.getExamInfo(
                loginInfo.getUsername(),
                loginInfo.getStudentName(),
                loginInfo.getXn(),
                loginInfo.getXq(),
                loginInfo.getSessionId()
        );

        return examInfo;
    }

    /**
     * @Date 2021-8-8
     * @author onestep
     * @Description 查询补考考试安排
     * */
    @PostMapping("/getexamagaininfo")
    public ExamInfo getExamAgainInfo(@RequestBody LoginInfo loginInfo) throws UnsupportedEncodingException {
        ExamInfo examInfo = jwService.getExamAgainInfo(
                loginInfo.getUsername(),
                loginInfo.getStudentName(),
                loginInfo.getXn(),
                loginInfo.getXq(),
                loginInfo.getSessionId()
        );

        return  examInfo;
    }

    /**
     * 查询成绩方法
     * */
    @PostMapping("/getscrodinfo")//这里有个坑，前端retrofit想要通过body  json传参数，这个API应该必须是POST
    public ScoresInfo getScrodInfo(@RequestBody LoginInfo loginInfo) throws UnsupportedEncodingException {
         
        ScoresInfo result = jwService.getScrodInfo(loginInfo.getUsername(),loginInfo.getStudentName(),loginInfo.getXn(),loginInfo.getXq(),loginInfo.getSessionId());

        return result;
    }

}
